package com.car.advertising.util;

import android.text.TextUtils;

import java.text.NumberFormat;

/**
 * 开发者：LuoYi
 * Time: 2017 23:56 2017/4/23 04
 */

public class AmountCalculationUtil {

    /**
     * 求两位数的百分比
     *
     * @param total
     * @param forNumber
     * @return
     */
    public static int getPercentage(String total, String forNumber) {
        if (TextUtils.isEmpty(total) || TextUtils.isEmpty(forNumber)) {
            return 0;
        }
        int totalInt = Integer.parseInt(total);
        int forNumberInt = Integer.parseInt(forNumber);
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(0);
        String result = numberFormat.format((float) forNumberInt / (float) totalInt * 100);
        if (TextUtils.isEmpty(result)) {
            return 0;
        }
        return Integer.parseInt(result);
    }

    /**
     * 取剩余人数
     *
     * @param total
     * @param forNumber
     * @return
     */
    public static int getSubtractionAmout(String total, String forNumber) {
        if (TextUtils.isEmpty(total) || TextUtils.isEmpty(forNumber)) {
            return 0;
        }
        int totalInt = Integer.parseInt(total);
        int forNumberInt = Integer.parseInt(forNumber);
        return totalInt - forNumberInt;
    }

}
